package models.user;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;

/**
 * 用户基本资料表
 * 
 * @author zhangpeng
 * 
 */

@Entity
@Table(name = "base_user_info")
public class BaseUserInfo extends Model {

	private static final long serialVersionUID = 1L;

	@Id
	public long id;
	public String username;
	public String realname;
	public String gender;
	public String birth;
	/**
	 * 出生地
	 */
	public String birth_addr;
	/**
	 * 居住地
	 */
	public String domicile;

	public static Model.Finder<Long, BaseUserInfo> find = new Model.Finder<Long, BaseUserInfo>(Long.class, BaseUserInfo.class);

	public static BaseUserInfo getInstances() {
		return new BaseUserInfo();
	}

	/**
	 * 用户基本信息保存
	 * 
	 * @param bui
	 * @return
	 * @throws MengException
	 */
	public boolean createBUI(BaseUserInfo bui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(bui.username)) {
			throw new MengException("200001", "用户名不存在");
		}

		if (verifyBaseUserInfo(bui.username)) {
			throw new MengException("200007", "用户基本信息已存在");
		}

		try {
			bui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createBUI Exception," + t.getMessage());
			throw new MengException("200002", "用户基本信息保存失败");
		}

	}
	
	public boolean createBUI(UserInfo ui) throws MengException {
		
		if (verifyBaseUserInfo(ui.username)) {
			throw new MengException("200008", "用户基本信息已存在");
		}
		BaseUserInfo bui  = getInstances();
		bui.username = ui.username;
		try {
			bui.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createBUI Exception," + t.getMessage());
			throw new MengException("200009", "用户基本信息保存失败");
		}
	}

	/**
	 * 用户基本信息修改
	 * 
	 * @param bui
	 * @return
	 * @throws MengException
	 */
	public boolean modifyBUI(BaseUserInfo bui) throws MengException {

		if (UserInfo.getInstances().verifyUsername(bui.username)) {
			throw new MengException("200003", "用户名不存在");
		}
		bui.id = getBaseUserInfo(bui.username).id;

		try {
			bui.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyBUI Exception," + t.getMessage());
			throw new MengException("200004", "用户基本信息修改失败");
		}
	}

	/**
	 * 获取用户基本信息
	 * 
	 * @param username
	 * @return
	 * @throws MengException
	 */
	public BaseUserInfo getBaseUserInfo(String username) throws MengException {

		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("200005", "用户名不存在");
		}

		try {

			return find.where().eq("username", username).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getBaseUserInfo Exception," + t.getMessage());
			throw new MengException("200006", "获取用户基本信息失败");
		}
	}

	public boolean verifyBaseUserInfo(String username) throws MengException {

		if (getBaseUserInfo(username) != null) {
			return true;
		}

		return false;
	}

}
